home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: what happens w/delete called twice ?
- Date: Fri, 23 Feb 1996 15:58:43 GMT
- Organization: Netcom
- Message-ID: <312dcc35.138429140@nntp.ix.netcom.com>
- References: <kcc.423.0EE12CF6@interaccess.com>
- NNTP-Posting-Host: ix-dc11-06.ix.netcom.com
- X-NETCOM-Date: Fri Feb 23 7:58:29 AM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- kcc@interaccess.com (kcc) wrote:
-
- > Hello,
- >
- > Can someone tell me... what happens when delete is called twice for the same
- > memory deallocation ie.,
- >
- > SOME_STRUCT *struct_p = new SOME STRUCT;
- >
- > if ( foo_a() )
- > {
- > delete struct_p;
- > retvalue = 0;
- > }
- > else
- > {
- > retvalue = foo_b();
- > }
- >
- > delete struct_p;
- >
- > return( retvalue );
-
- According to the draft this results in undefined behavior.
-
- In practice it is very unlikely that anything good will happen. With
- many (most) compilers you will corrupt the heap resulting in bizarre
- behavior later in the program.
-
- Don't do it.
-
- Note that delete of the null pointer is always legal and harmless so
- you can protect yourself in situations where it is not convenient to
- ensure only a single delete is done simply by assigning 0 to the
- pointer variable.
-
- Michael M Rubenstein
-